package com.monkeyboy.dubbo.service.impl;

import java.util.ArrayList;
import java.util.Map;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.monkeyboy.dubbo.api.IUserService;
import com.monkeyboy.dubbo.dao.BaseDao;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * timeout:级别超时设置<br>
 * version:版本号设置（基于灰度发布服务）<br>
 * stub:存根，用于调用服务前的处理（主要用于调用失败时候的自定义返回结果，便于消费方理解）<br>
 * weight: 权重（默认为100），用于负载调度（默认为随机） <br>
 * loadbalance:指定负载算法(按照权重进行轮询)，roundrobin->轮询<br>
 *
 * @author Gavin<br>
 *         2019年12月27日
 */
@Service(timeout = 3000, version = "1.0.0", weight = 10, loadbalance = "roundrobin",connections = 500)
public class UserServiceImpl implements IUserService {
	@Autowired
	private BaseDao baseDao;

	/**
	 * @HystrixCommand：支持服务熔断<br>
	 */
	@HystrixCommand
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Map<String, Object>> getAllUser() throws Exception {
		System.err.println("进入【老】服务");
		// Thread.sleep(5000);//超时测试
		if (Math.random() > 0.5) {// 测试服务容错，降级
			throw new RuntimeException("模拟服务降级，容错异常出现");
		}
		return (ArrayList<Map<String, Object>>) baseDao.findForList("UserMapper.getAllUser", null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getUserByUserName(String userName) throws Exception {
		System.err.println(userName);
		return (Map<String, Object>) baseDao.findForObject("UserMapper.getUserByUserName", userName);
	}

}
